package neusoft.yuejian_11.model;

import neusoft.yuejian_11.base.BaseActivity;
import neusoft.yuejian_11.bean.AddPrize;
import neusoft.yuejian_11.bean.AddpostBean;
import neusoft.yuejian_11.bean.CommentBean;
import neusoft.yuejian_11.bean.PostBean;
import neusoft.yuejian_11.interF.Const;
import neusoft.yuejian_11.interF.RetrofitService;
import neusoft.yuejian_11.listener.RetrofitListener;

import java.util.List;

import okhttp3.MultipartBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Created by NEUSOFT on 2017/9/28.
 */

public class PostModel extends BaseActivity{
    private Retrofit retrofit;
    public PostModel(){
        retrofit = new Retrofit.Builder()
                .baseUrl(Const.BASE_URL)
                .addConverterFactory(ScalarsConverterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();


    }



     //所有帖子列表
    public void  getPostlist(final RetrofitListener listener){
        RetrofitService retrofitService = retrofit.create(RetrofitService.class);
        Call<List<PostBean>> call = retrofitService.getPostlist();
        Callback<List<PostBean>> callback = new Callback<List<PostBean>>() {
            @Override
            public void onResponse(Call<List<PostBean>> call, Response<List<PostBean>> response) {
                List<PostBean> postBean = response.body();
                listener.onSuccess(postBean,1);
            }
            @Override
            public void onFailure(Call<List<PostBean>> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }


    //获取帖子详情
    public void getPostByIdDetail(String postId,final RetrofitListener listener) {
        RetrofitService retrofitService=retrofit.create(RetrofitService.class);
        Call call = retrofitService.getPostById(String.valueOf(postId));
        call.enqueue(new Callback<PostBean>() {
            @Override
            public void onResponse(Call<PostBean> call, Response<PostBean> response) {
                PostBean postBean=response.body();
                listener.onSuccess(postBean,1);
            }
            @Override
            public void onFailure(Call<PostBean> call, Throwable t) {
                listener.onFail();
            }
        });
    }



    //获取某个俱乐部帖子列表
    public void getCPost(String clubId,final RetrofitListener listener) {
        RetrofitService retrofitService=retrofit.create(RetrofitService.class);
        Call<List<PostBean>> call=retrofitService.getCPost(String.valueOf(clubId));
       call.enqueue(new Callback<List<PostBean>>() {
           @Override
           public void onResponse(Call<List<PostBean>> call, Response<List<PostBean>> response) {
               List<PostBean> postBean = response.body();
               listener.onSuccess(postBean, 1);
           }

           @Override
           public void onFailure(Call<List<PostBean>> call, Throwable t) {

           }
       });
    }



    //获取某条帖子的评论
    public void  getCommentPost(String postId,final RetrofitListener listener){
        RetrofitService retrofitService = retrofit.create(RetrofitService.class);
        Call<List<CommentBean>> call = retrofitService.getCommentPost(String.valueOf(postId));
        Callback<List<CommentBean>> callback = new Callback<List<CommentBean>>() {
            @Override
            public void onResponse(Call<List<CommentBean>> call, Response<List<CommentBean>> response) {
                List<CommentBean> commentBean = response.body();
                listener.onSuccess(commentBean,2);
            }
            @Override
            public void onFailure(Call<List<CommentBean>> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }



    //给某条晒图添加评论
    public void addcomment(int u_id, String postId, String coinfo,final RetrofitListener listener) {
        RetrofitService retrofitService=retrofit.create(RetrofitService.class);
        Call call=retrofitService.addcomment(getU_id(),String.valueOf(postId),coinfo);
        call.enqueue(new Callback<AddPrize>() {
            @Override
            public void onResponse(Call<AddPrize> call, Response<AddPrize> response) {
                AddPrize addPrize=response.body();
                listener.onSuccess(addPrize,5);
            }
            @Override
            public void onFailure(Call<AddPrize> call, Throwable t) {
                listener.onFail();
            }
        });
    }
    //搜索晒图
    public void getPostBySearch(String p_title, final RetrofitListener listener) {
        RetrofitService retrofitService = retrofit.create(RetrofitService.class);
        Call<List<PostBean>> call = retrofitService.getPostBySearch(p_title);
        Callback<List<PostBean>> callBack = new Callback<List<PostBean>>() {
            @Override
            public void onResponse(Call<List<PostBean>> call, Response<List<PostBean>> response) {
                List<PostBean> postBean = response.body();
                listener.onSuccess(postBean,1);

            }

            @Override
            public void onFailure(Call<List<PostBean>> call, Throwable t) {
                listener.onFail();

            }
        };
        call.enqueue(callBack);
    }
//发表晒图
public Call<AddpostBean>  addpost(int u_id, String p_info, MultipartBody.Part file)  {
    RetrofitService retrofitService=retrofit.create(RetrofitService.class);
    return retrofitService.addpost(u_id,p_info,file);
}

}



